<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .banner {
            width: 1200px;
            height: 600px;
            overflow: hidden;
            position: relative;
        }

        .box {
            width: 7200px;
            display: flex;
            position: absolute;
            left: 0;
        }

        .box div {
            width: 1200px;
            height: 600px;
            border: 5px black solid;
            box-sizing: border-box;
            font-size: 100px;
            line-height: 600px;
            text-align: center;
        }

        .dot {
            width: 1200px;
            display: flex;
            justify-content: center;
            position: absolute;
            bottom: 0;
        }

        .dot li {
            width: 50px;
            height: 50px;
            margin: 20px;
            border-radius: 50%;
            border: 5px black solid;
            box-sizing: border-box;

        }

        .show {
            background-color: grey;
        }

        .prev {
            height: 100px;
            font-size: 50px;
            line-height: 100px;
            position: absolute;
            left: 0;
            top: 48%;
        }

        .next {
            height: 100px;
            font-size: 50px;
            line-height: 100px;
            position: absolute;
            right: 0;
            top: 48%;
        }
    </style>
</head>

<body>


    <div class="banner">
        <div class="box">
            <div>1</div>
            <div>2</div>
            <div>3</div>
            <div>4</div>
            <div>5</div>
            <div>1</div>
        </div>
        <button class="prev">&lt;</button>
        <button class="next">&gt;</button>
        <ul class="dot">
            <li class="show"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>

    <script src="../js/jq.js"></script>
    <script>

        class Banner {
            constructor(ele) {
                this.ele = $(ele) ;
                this.oBox = this.ele.find('.box') ;
                this.oImg = this.oBox.find('div') ;
                this.oDots = this.ele.find('li') ;
                this.oPrev = this.ele.find('.prev');
                this.oNext = this.ele.find('.next');
                this.w = this.ele.innerWidth() ;
                this.index = 0 ;
                this.t = null ;
                this.flag = true ;
                this.autoPlay() ;
                this.click()

            }
            
            autoPlay() {
                this.t = setInterval(()=>{
                    this.index++ ;
                    this.flag = false ;
                    this.oBox.animate({
                        'left' : - this.index * this.w 
                    } , () => {
                        this.flag = true ;
                        if(this.index === this.oImg.length - 1) {
                            this.index = 0 ;
                            this.oBox.css('left' , 0) ;
                        }
                    })
                    if(this.index === this.oImg.length - 1) {
                        this.oDots.removeClass('show').eq(0).addClass('show');
                    } else {
                        this.oDots.removeClass('show').eq(this.index).addClass('show');
                    }
                    
                } , 2000)
            }


            click() {
                this.oPrev.on('click' , () => {
                    if(this.flag) {
                        clearInterval(this.t)
                        if(this.index == 0) {
                            this.index = this.oImg.length - 1 ;
                            this.oBox.css('left' , -this.index * this.w)
                        }
                    
                        this.index--;
                        this.flag = false ;
                        this.oBox.animate({
                            'left' : - this.index * this.w 
                        } , () => {
                            this.flag = true;
                        })
                        if(this.index === this.oImg.length - 1) {
                            this.oDots.removeClass('show').eq(0).addClass('show');
                        } else {
                            this.oDots.removeClass('show').eq(this.index).addClass('show');
                        }
                    }
                })

                this.oNext.on('click' , () => {
                    if(this.flag) {
                        clearInterval(this.t)
                        if(this.index == this.oImg.length - 1) {
                            this.index = 0 ;
                            this.oBox.css('left' , -this.index * this.w)
                        }
                    
                        this.index++;
                        this.flag = false ;
                        this.oBox.animate({
                            'left' : - this.index * this.w 
                        },() => {
                            this.flag = true;
                        })
                        if(this.index === this.oImg.length - 1) {
                            this.oDots.removeClass('show').eq(0).addClass('show');
                        } else {
                            this.oDots.removeClass('show').eq(this.index).addClass('show');
                        }
                    }
                })

                const _this = this;
                
                this.oDots.on('click' ,function() {

                    // 事件处理函数   this是指向小点
                    // console.log($(this).index());
                    
                    if(_this.flag) {
                        clearInterval(_this.t) ;
                        _this.index = $(this).index() ;



                        _this.oBox.animate({
                            'left' : - _this.index * _this.w 
                        },() => {
                            _this.flag = true;
                        })
                        if(_this.index === _this.oImg.length - 1) {
                            _this.oDots.removeClass('show').eq(0).addClass('show');
                        } else {
                            _this.oDots.removeClass('show').eq(_this.index).addClass('show');
                        }
                    }
                })



            }


        }


        new Banner('.banner')

    </script>
</body>

</html>